﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PicturePortfolio.ServiceContracts;
using PicturePortfolio.Domain;
using PicturePortfolio.Persistance;
using PicturePortfolio.Logging;
namespace PicturePortfolio.Service
{
    public class PhotoService:IPhotoService
    {
        public List<Photo> GetAllPhotos()
        {
            try
            {
                IRepository repository = new GenericRepository();
                List<PhotoPersistanceObject> photoPersistanceObjs = repository.GetAll<PhotoPersistanceObject>().ToList();
                return this.CreatePhotoList(photoPersistanceObjs);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "GetAllPhotos");
                throw;
            }
        }

        public Photo GetPhoto(Photo photo)
        {
            try
            {
                photo.LoadImage();
                return photo;
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                throw;
            }
        }

        public void SavePhoto(Photo photo)
        {
            try
            {
                if (photo.ImageFile == null)
                    throw new ArgumentNullException();
                IRepository repository = new GenericRepository();
                repository.Add<PhotoPersistanceObject>(photo.GetDataForPersistance());
                photo.SaveImage();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, photo.Name);
                throw;
            }
        }

        public void DeletePhoto(Photo photo)
        {
            try
            {
                IRepository repository = new GenericRepository();
                repository.Delete<PhotoPersistanceObject>(photo.GetDataForPersistance());
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, photo.GUID);
                throw;
            }
        }


        public void UpdatePhoto(Photo photo)
        {
            IRepository repository = new GenericRepository();
            PhotoPersistanceObject photoOld = repository.First<PhotoPersistanceObject>(p => p.Name == photo.Name);
            repository.Update<PhotoPersistanceObject>(photo.GetDataForPersistance(), photoOld);
        }


        public void SavePhotos(List<Photo> photos)
        {
            foreach (Photo photo in photos)
            {
                this.SavePhoto(photo);
            }
        }


        public List<Category> GetCategories()
        {
            try
            {
                IRepository repository = new GenericRepository();
                List<Category> categories = repository.GetAll<Category>().ToList();
                try
                {
                    foreach (Category category in categories)
                    {

                        if (category.CategoryThumbNail.Length < 2)
                        {
                            PhotoPersistanceObject tempObj = repository.First<PhotoPersistanceObject>(p => p.PhotoCategory.GUID == category.GUID);
                            if (tempObj != null)
                            {
                                Photo photo = new Photo(tempObj);
                                category.CategoryThumbNail = photo.ImageThumbNail;
                            }
                        }
                    }
                }
                catch (Exception exIgnore)
                {

                }

                return categories;
            }
            catch (Exception ex)
            {
                Logger.LogError(ex,"GetCategories");
                throw;
            }
        }


        public void SaveCategory(Category category)
        {
            try
            {
                IRepository repository = new GenericRepository();
                Category categoryToSave = new Category();
                categoryToSave.CategoryName = category.CategoryName;
                repository.Add<Category>(categoryToSave);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }


        public List<Photo> GetPhotosByCategory(Category category)
        {
            try
            {
                IRepository repository = new GenericRepository();
                List<PhotoPersistanceObject> photoPersistanceObjects=null;
                if (category == null)
                    photoPersistanceObjects = repository.GetAll<PhotoPersistanceObject>().ToList();
                else
                    photoPersistanceObjects = repository.Find<PhotoPersistanceObject>(p => p.PhotoCategory.GUID == category.GUID).ToList();
                return this.CreatePhotoList(photoPersistanceObjects);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, category.CategoryName);
                throw;
            }
        }

        private List<Photo> CreatePhotoList(List<PhotoPersistanceObject> photoPersistanceObjects)
        {

            List<Photo> photos = new List<Photo>();
            try
            {
                foreach (PhotoPersistanceObject persistanceObject in photoPersistanceObjects)
                {
                    Photo photo = new Photo(persistanceObject);
                    photos.Add(photo);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                throw;
            }
            return photos;
        }


        public void DeleteCategory(Category category)
        {
            IRepository repository = new GenericRepository();
            repository.Delete<Category>(category);
        }


        public List<PublicComment> GetCommentsByPhoto(Photo photo)
        {
            try
            {
                IRepository repository = new GenericRepository();
                return repository.Find<PublicComment>(s => s.Photo.GUID == photo.GUID).ToList();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, photo.GUID);
                throw;
            }
        }

        public void DeleteComment(PublicComment comment)
        {
            throw new NotImplementedException();
        }

        public void AddComment(PublicComment comment, Photo photo)
        {
            try
            {
                IRepository repository = new GenericRepository();
                comment.Photo = photo.GetDataForPersistance();
                repository.Add<PublicComment>(comment);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, photo.GUID);
                throw;
            }
        }
    }
}
